﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Practices.Unity;
using System.Transactions;
using System.Data;
using Skynet.BusinessEntities;

namespace Skynet.BusinessLogic.Test.DataAccess
{
    [TestClass]
    public class ExamCategoryTest
    {

        public  static IUnityContainer Container;

        [ClassInitialize()]
        public static void Initialize(TestContext testContext) {

            Container = UnityHelper.BuildUnityContainer();  
        }
        
        [TestMethod]
        public void BulkInsert()
        {           
            
            //System.Threading.Tasks.Parallel.For(1, 20000, delegate(long l){
            //    var rep = Container.Resolve<BusinessLogic.DataAccess.IExamCategoryRepository>();
            //    var item = new BusinessEntities.ExamCategory() { Id = Guid.NewGuid(), Title = "paralel", Description = "paralel" }; 
            //    rep.Add(item);
                
            //} );           
        }
        [TestMethod]
        public void BulkLoad()
        {

            System.Threading.Tasks.Parallel.For(1, 2000000, delegate(long l)
            {
                var rep = Container.Resolve<BusinessLogic.DataAccess.IExamCategoryRepository>();                
                rep.GetAll();
            });
        }   

        [TestMethod]
        public void StreamReader()
        {
            var rep = Container.Resolve<BusinessLogic.DataAccess.IExamCategoryRepository>();

            System.Threading.Tasks.Parallel.For(1, 200, delegate(int i) 
                { 
                    var reader = rep.GetAllStream();
                    while (reader.Read())
                    { 

                    }                        
                });
        }
        [TestMethod]
        public void AddDistributeTransaction()
        {            
            //var rep = Container.Resolve<BusinessLogic.DataAccess.IExamCategoryRepository>();
            //Guid cid = Guid.NewGuid(); 
            //using (var scope = new TransactionScope())
            //{
            //    rep.Add(new BusinessEntities.ExamCategory() { Id = cid, Title = "paralel", Description = "paralel" });
            //    for (int i = 0; i < 200000; i++)
            //    {                    
            //        rep.Add(new BusinessEntities.ExamCategory() { Id = Guid.NewGuid(), Title = "paralel", Description = "paralel" });     
            //    }
            //    scope.Complete();
            //}
            //var target = rep.GetOne(cid);
            //Assert.IsNotNull(target); 
        }
        [TestMethod]
        public void RollbackDistributeTransaction()
        {
            //var rep = Container.Resolve<BusinessLogic.DataAccess.IExamCategoryRepository>();
            //Guid cid = Guid.NewGuid(); 
            //using (var scope = new TransactionScope())
            //{
            //    rep.Add(new BusinessEntities.ExamCategory() { Id = cid, Title = "paralel", Description = "paralel" });
            //    for (int i = 0; i < 20000; i++)
            //    {
            //        rep.Add(new BusinessEntities.ExamCategory() { Id = Guid.NewGuid(), Title = "paralel", Description = "paralel" });
            //    }             
            //}
            //var target=  rep.GetOne(cid);
            //Assert.IsNull(target); 
        }

        [TestMethod]
        public void Loads()
        {
            var rep = Container.Resolve<BusinessLogic.DataAccess.IExamCategoryRepository>();
            
            for (int i = 0; i < 2000; i++)
            {
                rep.GetAll();                     
            }            
        }
        [TestMethod]
        public void ExamCategoryPCRUDLOAD()
        {
            
                System.Threading.Tasks.Parallel.For(0, 400, delegate(int param)
                {
                    var opt = new TransactionOptions();
                    opt.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted;
                    
                    //using (var scope = new TransactionScope (TransactionScopeOption.Required,   opt))
                    //{
                        var rep = Container.Resolve<BusinessLogic.DataAccess.IExamCategoryRepository>();
                        for (int i = 0; i < 5; i++)
                        {
                            var resul = rep.GetAll();
                            foreach (var item in resul)
                            {
                            }
                            CRUD();
                         }
                    //    scope.Complete();
                    //}
                });
            
            
        }

        [TestMethod]
        public void CRUD()
        {
            //var examcat = new BusinessEntities.ExamCategory();
            //examcat.Id = Guid.NewGuid();  
            //examcat.Title = "testTitle";
            //examcat.Description = "Description Test";

            //var rep = Container.Resolve<BusinessLogic.DataAccess.IExamCategoryRepository>();
            //rep.Add(examcat);

            //var target =  rep.GetOne (examcat.Id);

            //Assert.IsNotNull(target);  

            //examcat.Title =  "change title";
            //examcat.Description = "change description";

            //rep.Update(examcat);

            //target = rep.GetOne(examcat.Id);
            //Assert.IsNotNull(target);
            //Assert.AreEqual<string>("change title", target.Title);

            //rep.Delete(examcat);
            //target = rep.GetOne(examcat.Id);

            //Assert.IsNull(target);  
        }
        [TestMethod]
        public void DataSetTest()
        {
            var rep = Container.Resolve<BusinessLogic.DataAccess.IExamCategoryRepository>();

            var ds = new DataSet();
            ds.Merge(rep.GetTable(), true,  MissingSchemaAction.Add);

            var r = ds.Tables[0].NewRow();
            r["Id"] = Guid.NewGuid();
            r["Title"] = "dataset";
            r["Description"] = "dataset";

            ds.Tables[0].Rows.Add(r);

            rep.UpdateTable(ds.Tables[0]);


            var rows =  ds.Tables[0].Select( "Id='" + r["Id"].ToString()+ "'");
            var row = rows.SingleOrDefault();
            row["Title"] = "change dataset";

            rep.UpdateTable(ds.Tables[0]);

            var dstarget = new DataSet();
            dstarget.Merge(rep.GetTable(), true, MissingSchemaAction.Add);
            var targetrows = dstarget.Tables[0].Select("Id='" + r["Id"].ToString() + "'"); 
            var targetrow = rows.SingleOrDefault();

            Assert.AreEqual<string>("change dataset", targetrow["Title"].ToString());

            r.Delete();  

            rep.UpdateTable(ds.Tables[0]);

            dstarget = new DataSet();
            dstarget.Merge(rep.GetTable(), true, MissingSchemaAction.Add);
            try
            {
                targetrows = dstarget.Tables[0].Select("Id='" + r["Id"].ToString() + "'");
                Assert.Fail("delete dont success");  
            }
            catch (Exception)
            {                
                    
            }                        

            














        }

        [TestMethod]
        public void OptimisticTest()
        {

            
            var exc = new ExamCategory();
            exc.Id = Guid.NewGuid();
            exc.Title = "optimistic";
            exc.Description = "demostracion";
            var rep = Container.Resolve<Skynet.BusinessLogic.DataAccess.IExamCategoryRepository>();

            rep.Maintenance(exc);  
            


 
        }

        [ClassCleanup()]
        public static void Cleanup() { 



        }
    }
}
